/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.validators;

import java.util.Iterator;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.Reception;
import org.eclipse.uml2.uml.Signal;

import com.cea.papyrus.core.PapyrusPlugin;


/**
 * A validator class for transformation selection dialog
 *
 */
public class MessageSignalSelectionStatusValidator implements
		ISelectionStatusValidator {

	private Message message;
	
	public MessageSignalSelectionStatusValidator(Message message) {
		super();
		this.message = message;
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.ISelectionStatusValidator#validate(java.lang.Object[])
	 */
	public IStatus validate(Object[] arg0) {


		if ((arg0 != null) && (arg0.length > 0)) {
			// Single selection
			Object selection = arg0[0];

			if (! (selection instanceof Signal)) {
				return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "No signal selected.");
			}

			Signal currentSig = (Signal) selection;		
						
			if ((message.getReceiveEvent() != null)
					&& (message.getReceiveEvent() instanceof MessageOccurrenceSpecification)) {
				
				MessageOccurrenceSpecification recvEnd = 
					(MessageOccurrenceSpecification) message.getReceiveEvent();
				Lifeline covered = recvEnd.getCovereds().get(0);
				
				if ((covered.getRepresents() != null) 
						&& (covered.getRepresents().getType() != null)
						&& (covered.getRepresents().getType() instanceof Class)) {
							
						
					Class expectedType = (Class) covered.getRepresents().getType();
					Iterator<Reception> i = expectedType.getOwnedReceptions().iterator();
					boolean receptionfound = false;
					while(i.hasNext()) {
						Reception current = i.next();
						if (current.getSignal().equals(currentSig)) {
							receptionfound = true;
						}
					}
					
					if (! receptionfound) {
						return new Status(
								IStatus.WARNING, 
								PapyrusPlugin.PLUGIN_ID, 
								"No reception for signal "+currentSig.getName()+" on class "+expectedType.getName()+".");
					}
				}
			}
						
			return Status.OK_STATUS;

		} else {
			// No selection
			return new Status(IStatus.WARNING, PapyrusPlugin.PLUGIN_ID, "Empty selection.");
		}

		//return new Status(IStatus.ERROR, PapyrusPlugin.PLUGIN_ID, "Unknown error.");
	}

}
